var SysConfigModel = require('../models/sysConfigModel')
var async = require('async');


var sysConfigCtrl = {};

//
sysConfigCtrl.get = function (req, res) {


    var skey = req.query.skey;
    var page = req.query.page || 1;
    var queryCondition = {};

    if (skey) {
        queryCondition.skey = {
            $regex: new RegExp(skey, "i")
        };
    }

    async.parallel({

        //
        count: function (done) {  // 查询数量
            SysConfigModel.count(queryCondition).exec(function (err, count) {
                done(err, count);
            });
        },

        //
        records: function (done) {   // 查询一页的记录
            SysConfigModel.find(queryCondition).sort({
                'ctime': -1
            }).skip((page - 1) * 10).limit(10).exec(function (err, docs) {
                done(err, docs);
            });
        },

    }, function (err, results) {

        var count = results.count;
        var records = results.records;
        if (err) {

            res.status(500).json({
                code: -1,
                msg: '没有对应的信息'
            })
        } else {

            res.json({
                code: 0,
                dat: {
                    total: count,
                    records: records
                }
            })
        }

    });


};

//
sysConfigCtrl.getOne = function (req, res) {

    var _id = req.query._id;
    SysConfigModel.findOne({ _id: _id }, function (err, item) {

        if (err) {

            res.status(500).json({
                code: -1,
                msg: '没有对应的信息'
            })
        } else {

            res.json({
                code: 0,
                dat: item
            })
        }

    });

};


//
sysConfigCtrl.add = function (req, res, next) {

    var data = req.body
    SysConfigModel.create(data, function (err, item) {

        if (err) {

            console.log(err)
            res.status(500).json({
                code: -1,
                msg: '保存数据错误'
            })
        } else {
            res.json({
                code: 0,
                dat: item
            })
        }

    });

}

//
sysConfigCtrl.update = function (req, res, next) {

    var data = req.body
    var _id = data._id

    SysConfigModel.findByIdAndUpdate(_id, data, { new: true }, function (err, item) {

        if (err) {

            console.log(err)

            res.status(500).json({
                code: -1,
                msg: '保存数据错误'
            })
        } else {
            res.json({
                code: 0,
                dat: item
            })
        }

    });

}

//
sysConfigCtrl.remove = function (req, res, next) {

    var _id = req.query._id;
    SysConfigModel.findByIdAndRemove(_id, function (err, item) {

        if (err) {

            console.log(err)

            res.status(500).json({
                code: -1,
                msg: '保存数据错误'
            })
        } else {
            res.json({
                code: 0,
                dat: item
            })
        }

    });

}

module.exports = sysConfigCtrl;